package com.chuan.it_market.controller.seller_controller;

import com.chuan.it_market.controller.BaseController;
import com.chuan.it_market.pojo.dto.ResponseDTO;
import com.chuan.it_market.pojo.entity.Goods;
import com.chuan.it_market.pojo.entity.GoodsType;
import com.chuan.it_market.service.GoodsService;
import com.chuan.it_market.service.TypeService;
import org.springframework.boot.Banner;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/seller/goods")
public class GoodsController extends BaseController {

    @Resource
    TypeService typeService;
    @Resource
    GoodsService goodsService;

    @RequestMapping("goodsAddPre")
   String queryAllType(Model model){
        List<GoodsType> goodsTypeList=typeService.selectAllType();

        model.addAttribute("alltype",goodsTypeList);
        return "seller/goods/goods-addpre";
   }


    @RequestMapping("add")
    @ResponseBody
    ResponseDTO add(Goods goods, MultipartFile pic, HttpSession session){



        String imgUrl=uploadFile("/upload/goods/",pic);
        goods.setIcon(imgUrl);



        goods.setSno(getUsreId());


        return goodsService.add(goods);

    }

    @RequestMapping("update")
    @ResponseBody
    ResponseDTO update(Goods goods, MultipartFile pic){


        System.out.println(pic);
        if(!pic.isEmpty()){
            String imgUrl=uploadFile("/upload/goods/",pic);
            goods.setIcon(imgUrl);
        }




        return goodsService.update(goods);

    }

    //进入goods-list前把商品类型查出来 还有所有商品
    @RequestMapping("goodsList")
    String queryAllTypeAndGoods(Model model){
        List<GoodsType> goodsTypeList=typeService.selectAllType();
        Integer sno=getUsreId();
        List<Goods> goodsList=goodsService.selectAllGoods(sno);

        model.addAttribute("allgoods",goodsList);
        model.addAttribute("alltype",goodsTypeList);
        return "seller/goods/goods-list";
    }


    @RequestMapping("getGoodsByTnoAndSno/{typeno}")
    @ResponseBody
    ResponseDTO getGoodsByTno(@PathVariable Integer typeno){
            Integer sno=getUsreId();
            return goodsService.getGoodsByTnoAndSno(typeno,sno);
    }

    @RequestMapping("queryByInput")
    @ResponseBody
    ResponseDTO queryByInput(String name){

        Integer sno=getUsreId();
       List<Goods> goodsList2=goodsService.getGoodsByInput(name,sno);
       return ResponseDTO.ok("查询成功",goodsList2);
    }

      @RequestMapping("goodsDetail/{gno}")
      String goodsDetail(@PathVariable Integer gno, Model model){
        model.addAttribute("goods",goodsService.getGoodsDetail(gno));
        return "seller/goods/goods-detail";
      }

      @RequestMapping("goodsDelete/{gno}")
      @ResponseBody
      ResponseDTO goodsDetete(@PathVariable Integer gno){
        int i=goodsService.delete(gno);
        if (i!=0)
        {
            return ResponseDTO.ok("删除成功");
        }
        else{
            return ResponseDTO.fail("删除失败");
        }
      }

      @RequestMapping("goodsEditPre/{gno}")
    String goodEditPre(@PathVariable Integer gno,Model model){
        Goods goods1 =goodsService.getGoodsByPrimaryKey(gno);
        model.addAttribute("goods",goods1);
          List<GoodsType> goodsTypeList=typeService.selectAllType();
          model.addAttribute("alltype",goodsTypeList);
        return "/seller/goods/goods-editpre";
      }
}
